package com.gaojinqi.sys.modular.config.service.impl;

import com.gaojinqi.base.common.constant.RespConst;
import com.gaojinqi.base.common.enums.StateEnum;
import com.gaojinqi.base.common.exception.BizRuntimeException;
import com.gaojinqi.base.common.service.imp.BaseServiceImpl;
import com.gaojinqi.base.core.sys.config.ConfigContext;
import com.gaojinqi.base.core.sys.config.ConfigContextHolder;
import com.gaojinqi.sys.modular.config.dao.SysConfigRepository;
import com.gaojinqi.sys.modular.config.model.SysConfig;
import com.gaojinqi.sys.modular.config.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static com.gaojinqi.base.core.sys.config.ConfigConst.SYSTEM_CONSTANT_PREFIX;

/**
 * 系统配置业务逻辑层实现
 *
 * @author gaojinqi
 * @version 1.0
 * @since 2020年04月15日
 */
@Service
@Slf4j
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfig, String> implements SysConfigService {

    @Autowired
    private SysConfigRepository sysConfigRepository;

    @Override
    public List<SysConfig> findByEnableState() {
        return sysConfigRepository.findByState(StateEnum.ENABLE.getCode());
    }

    @Override
    @Transactional
    public void save(SysConfig entity) {
        //判断编码有没有重复
        SysConfig repeat = sysConfigRepository.findByConfigCode(entity.getConfigCode());
        if (repeat != null) {
            throw new BizRuntimeException(RespConst.RespEnum.CONFIG_REPEAT);
        }
        super.save(entity);
        ConfigContextHolder.getContext().put(entity.getConfigCode(), entity.getConfigValue());

    }

    @Override
    @Transactional
    public void update(SysConfig entity) {
        super.update(entity);
        ConfigContextHolder.getContext().put(entity.getConfigCode(), entity.getConfigValue());
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        SysConfig oldSysConfig = findById(id);
        if (oldSysConfig != null) {
            if (oldSysConfig.getConfigCode().startsWith(SYSTEM_CONSTANT_PREFIX)) {
                throw new BizRuntimeException(RespConst.RespEnum.SYS_CONFIG_NO_AOLLOW_DEL);
            }
            super.deleteById(id);
            ConfigContextHolder.getContext().remove(oldSysConfig.getConfigCode());
        }
    }

    @Override
    @Transactional
    public void deleteByIds(List<String> ids) {
        List<SysConfig> sysConfigList = sysConfigRepository.findByConfigIdIn(ids);
        if (!CollectionUtils.isEmpty(sysConfigList)) {
            ConfigContext configContext = ConfigContextHolder.getContext();
            super.deleteByIds(ids);
            for (SysConfig oldSysConfig : sysConfigList) {
                if (oldSysConfig.getConfigCode().startsWith(SYSTEM_CONSTANT_PREFIX)) {
                    throw new BizRuntimeException(RespConst.RespEnum.SYS_CONFIG_NO_AOLLOW_DEL);
                }
                configContext.remove(oldSysConfig.getConfigCode());
            }
            super.deleteByIds(ids);
        }
    }

    @Override
    public void reload() {
        List<SysConfig> sysConfigList = this.findByEnableState();
        if (!CollectionUtils.isEmpty(sysConfigList)) {
            ConfigContext configContext = ConfigContextHolder.getContext();
            ConfigContextHolder.getContext().clear();
            for (SysConfig sysConfig : sysConfigList) {
                configContext.put(sysConfig.getConfigCode(), sysConfig.getConfigValue());
            }
            if (log.isDebugEnabled()) {
                log.debug("重加载系统配置 {} 条", sysConfigList.size());
            }
        }
    }

}
